﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using pbvs.Admin;
using pbvs.Models;
using System.Web.Security;

namespace pbvs.Controllers
{
    public class ClientController : Controller
    {

        public PbvsMembershipProvider MembershipService { get; set; }
        public PbvsRoleProvider AuthorizationService { get; set; }

        protected override void Initialize(System.Web.Routing.RequestContext requestContext)
        {
            if (MembershipService == null)
            {
                MembershipService = new PbvsMembershipProvider();
            }
            if (AuthorizationService == null)
            {
                AuthorizationService = new PbvsRoleProvider();
            }
            base.Initialize(requestContext);
        }

        public ActionResult LogOn()
        {
            return PartialView();
        }

        [HttpPost]
        public ActionResult LogOn(Login login)
        {
            if (ModelState.IsValid)
            {
                if (MembershipService.ValidateUser(login.Email, login.Password))
                {
                    FormsAuthentication.SetAuthCookie(login.Email, false);
                    return RedirectToAction("Index", "Valuation");
                }
                else
                {
                    ModelState.AddModelError("", "The username and/or password entered does not match our records.");
                }
            }
            return View(login);
        }

        [Authorize(Roles = "Admin")]
        public string Test()
        {
            return "You are authorized";
        }

        public ActionResult LogOut()
        {
            FormsAuthentication.SignOut();
            return RedirectToAction("LogOn");
        }
    }
}
